package test.boid.vo;

import java.util.HashMap;

import com.jme.light.DirectionalLight;
import com.jme.math.Matrix3f;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;
import com.jme.scene.SharedNode;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Sphere;
import com.jme.scene.state.LightState;
import com.jme.scene.state.RenderState;
import com.jme.system.DisplaySystem;

import dviz.tools.ModelRepository;
import dviz.visualSystem.impl.AnimationObject;

/**
 * @author zxq071000
 */
public class SimpleCone extends AnimationObject {

	private float x;
	private float y;
	private float speedx;
	private float speedy;
	private float size;
	private float colorRed, colorBlue, colorGreen;

	public void setColorRed(float colorRed) {
		this.colorRed = colorRed;
	}

	public void setColorBlue(float colorBlue) {
		this.colorBlue = colorBlue;
	}

	public void setColorGreen(float colorGreen) {
		this.colorGreen = colorGreen;
	}

	public SimpleCone(HashMap<String, Object> initVector) {
		super(initVector);
		if (initVector.get("colorRed") != null)
			colorRed = new Float(initVector.get("colorRed").toString());
		if (initVector.get("colorGreen") != null)
			colorGreen = new Float(initVector.get("colorGreen").toString());
		if (initVector.get("colorBlue") != null)
			colorBlue = new Float(initVector.get("colorBlue").toString());
		ModelRepository modelRepository = ModelRepository.get();
		SharedNode node = new SharedNode((Node) modelRepository.getRepository()
				.get("cone"));
		// Sphere node = new Sphere("", new Vector3f(), 10, 10, 10);
		node.setLocalRotation(new Quaternion(new float[] {
				(float) Math.PI / 2f, 0, 0 }));
		node.setLocalScale(0.2f);
		getRenderNode().attachChild(node);

		LightState lightState = DisplaySystem.getDisplaySystem().getRenderer()
				.createLightState();
		lightState.setEnabled(true);

		DirectionalLight dl = new DirectionalLight();
		dl.setDirection(new Vector3f(1, 1, 1));
		dl.setDiffuse(new ColorRGBA(colorRed, colorGreen, colorBlue, 1));
		dl.setAmbient(new ColorRGBA(colorRed, colorGreen, colorBlue, 1));
		dl.setEnabled(true);

		lightState.attach(dl);

		getRenderNode().setRenderState(lightState);
	}

	/**
	 * @param size
	 * @uml.property name="size"
	 */
	public void setSize(float size) {
		this.size = size;
		getRenderNode().setLocalScale(size);
	}

	/**
	 * @param x
	 * @uml.property name="x"
	 */
	public void setX(float x) {
		this.x = x;
		getRenderNode().getLocalTranslation().setX(x);
	}

	/**
	 * @param y
	 * @uml.property name="y"
	 */
	public void setY(float y) {
		this.y = y;
		getRenderNode().getLocalTranslation().setZ(y);
	}

	/**
	 * @param speedx
	 * @uml.property name="speedx"
	 */
	public void setSpeedx(float speedx) {
		this.speedx = speedx;
		updateDirection();

	}

	/**
	 * @param speedy
	 * @uml.property name="speedy"
	 */
	public void setSpeedy(float speedy) {
		this.speedy = speedy;
		updateDirection();
	}

	private void updateDirection() {
		float x = speedx;
		float y = speedy;
		float len = (float) Math.sqrt(x * x + y * y);
		if (len < Float.MIN_VALUE)
			return;
		x /= len;
		y /= len;
		Matrix3f rot = new Matrix3f();
		Vector3f vY = new Vector3f(0, 1, 0);
		Vector3f vX = new Vector3f(y, 0, -x);
		Vector3f vZ = new Vector3f(x, 0, y);
		rot.fromAxes(vX, vY, vZ);
		getRenderNode().setLocalRotation(rot);
	}

}
